<template>
  <!-- 采购管理 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.search"
            placeholder="标题"
            clearable
          ></el-input>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
        stripe
        @row-dblclick="handleRowDblclick"
      >
        <!--         @row-click="handleRowClick"
        :cell-class-name="setCellStyle" -->
        <el-table-column label="ID" prop="id" width="50"></el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column
          label="采购部门"
          prop="procuredDepartmentName"
          width="200"
        ></el-table-column>
        <el-table-column
          label="采购人"
          prop="procuredAdminName"
          width="200"
        ></el-table-column>
        <el-table-column
          label="采购时间"
          prop="procuredTime"
          width="200"
        ></el-table-column>
        <el-table-column
          label="到货时间"
          prop="arrivalTime"
          width="200"
        ></el-table-column>
        <el-table-column label="采购金额" prop="procuredPrice" width="200">
          <template #default="scope">
            <p>{{ scope.row.procuredPrice }}元</p>
          </template>
        </el-table-column>
        <!-- <el-table-column label="采购物品" min-width="200">
          <template #default="scope">
            <p v-for="item in scope.row.procurementProductList" :key="item.id">
              {{ item.productName }}：{{ item.specificationName
              }}{{ item.productBuyNum }}{{ item.unitName }}
            </p>
          </template>
        </el-table-column> -->
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="审核状态" prop="status" width="150" sortable>
          <template #default="scope">
            <!--0，未审核，1,审核通过，2审核未通过-->
            <p v-if="scope.row.status == 0">待审核</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">通过</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">未通过</p>
            <p v-if="scope.row.status == 3" style="color: #ff9b31">审核中</p>
          </template>
        </el-table-column>
        <el-table-column label="到货状态" prop="arrivalState" width="200">
          <template #default="scope">
            <!--到货状态：0未到货 1已发货 2已到货 3已退货-->
            <p v-if="scope.row.arrivalState == 0">未到货</p>
            <p v-if="scope.row.arrivalState == 1">已发货</p>
            <p v-if="scope.row.arrivalState == 2">已到货</p>
            <p v-if="scope.row.arrivalState == 3">已退货</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="250">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_confirmDoods(scope.row, scope.$index)"
                v-if="scope.row.state == 1 && scope.row.arrivalState == 1"
                >确认到货</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                >查看</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-if="scope.row.state == 0"
                >编辑</el-button
              >
              <el-popconfirm
                title="确定作废吗？"
                @confirm="table_del(scope.row, scope.$index)"
                v-auth-button="['scrap/purchaseManagement']"
                v-if="scope.row.status != 0"
              >
                <template #reference>
                  <el-button text type="primary" size="small">作废</el-button>
                </template>
              </el-popconfirm>
              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
                v-if="scope.row.state == 0"
              >
                <template #reference>
                  <el-button text type="primary" size="small">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
</template>
<script>
export default {
  name: "purchaseRequest",
  data () {
    return {
      apiObj: this.$API.purchase.getPurchaseList,
      searchValue: {
        search: null,
      },
    }
  },

  methods: {
    //添加
    add () {
      this.$router.push({
        path: "/goods/purchaseManagement/addPurchase",
        query: {
          mode: "add",
        },
      })
    },

    //编辑
    table_edit (row) {
      if (row.state != 0) {
        this.$message.error(`该采购已经过审批，不能编辑`)
        return
      }
      this.$router.push({
        path: "/goods/purchaseManagement/addPurchase",
        query: {
          mode: "edit",
          id: row.id,
        },
      })
    },

    // 确认到货
    table_confirmDoods (row) {
      this.$router.push({
        path: "/goods/purchaseManagement/confirmGoods",
        query: {
          id: row.id,
        },
      })
    },

    //查看
    table_show (row) {
      this.$router.push({
        path: "/goods/purchaseManagement/purchaseDetail",
        query: {
          id: row.id,
        },
      })
    },
    //删除
    async table_del (row) {
      let data = {
        id: row.id,
      }
      var res = await this.$API.purchase.deletePurchase.post(data)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("操作成功")
      }
    },

    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },


    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    },
  },
};
</script>

<style></style>
